import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Utils } from '../../common/utils';
import { UiserviceProvider } from '../../providers/uiservice/uiservice';
import { BusiserviceProvider } from '../../providers/busiservice/busiservice';
import { LoadingUtil } from '../../common/loading';

/**
 * Generated class for the ChangepasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-changepassword',
  templateUrl: 'changepassword.html',
})
export class ChangepasswordPage {
  oldpassword: any;
  newpassword: any;
  confirmpassword: any;
  loading:LoadingUtil;
  constructor(public navCtrl: NavController,private loadingCtrl: LoadingController, public navParams: NavParams, private uiservice: UiserviceProvider, public busi: BusiserviceProvider) {
    this.loading = new LoadingUtil(this.loadingCtrl);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ChangepasswordPage');
  }
  submit() {
    if (Utils.isNullOrEmpty(this.oldpassword)) {
      this.uiservice.showAlert("老密码为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.newpassword)) {
      this.uiservice.showAlert("新密码为空！");
      return;
    }
    if (Utils.isNullOrEmpty(this.confirmpassword)) {
      this.uiservice.showAlert("确认密码为空！");
      return;
    }
    if(this.newpassword !=this.confirmpassword){
      this.uiservice.showAlert("新密码和确认密码不一致!");
      return;
    }
    this.loading.showLoading();
    this.busi.changePassword(this.newpassword,this.oldpassword).subscribe(
      data=>{
        this.loading.hideLoading();
        if(data._success){
          this.navCtrl.pop();
          this.uiservice.showToast("修改成功！");
        }else{
          this.uiservice.showAlert(data._message);
        }
      },
      error=>{
        this.uiservice.showAlert("网络异常!")
        this.loading.hideLoading();
      }
    );

  }

}
